/*******************************************
 *                                         *
 *******************************************/

#include "MainMenu.h"

MainMenu::MainMenu (void)
{
	this->Background = NULL;
	this->Title = NULL;
	this->PlayGame = NULL;
	this->Quit = NULL;
	this->SelectedText = "Play Game";
}

MainMenu::MainMenu (LPDIRECT3DDEVICE9 DrawDevice, float ScreenWidth, float ScreenHeight)
{
	this->Background = NULL;
	this->Title = NULL;
	this->PlayGame = NULL;
	this->Quit = NULL;
	this->SelectedText = "Play Game";
	this->Init(DrawDevice, ScreenWidth, ScreenHeight);
}

MainMenu::~MainMenu (void)
{
	if (this->Background)
		delete this->Background;
	if (this->Title)
		delete this->Title;
	if (this->PlayGame)
		delete this->PlayGame;
	if (this->Quit)
		delete this->Quit;
}

BOOL MainMenu::Init (LPDIRECT3DDEVICE9 DrawDevice, float ScreenWidth, float ScreenHeight)
{
	BOOL Success = TRUE;

	if (!(this->Background = new Sprite(DrawDevice, ScreenWidth, ScreenHeight, 1.0f)))
		Success = FALSE;
	else
		this->Background->LoadTexture(L"MainMenu.png");
	if ((Success) && (!(this->Title = new Text(DrawDevice, (UINT)ScreenWidth / 12, TRUE, L"Times New Roman"))))
		Success = FALSE;
	else
	{
		this->Title->SetText("GDC Tetris");
		this->Title->SetColor(COLOR(200, 0, 0));
		this->Title->SetLocation((UINT)((ScreenWidth / 2) - (ScreenWidth / 6)), (UINT)((ScreenHeight / 2) - (ScreenHeight / 3)));
	}
	if ((Success) && (!(this->PlayGame = new Text(DrawDevice, (UINT)ScreenWidth / 22, FALSE, L"Times New Roman"))))
		Success = FALSE;
	else
	{
		this->PlayGame->SetText("Play Game");
		this->PlayGame->SetColor(COLOR(0, 255, 0));
		this->PlayGame->SetLocation((UINT)((ScreenWidth / 2) - (ScreenWidth / 12)), (UINT)((ScreenHeight / 2)));
	}
	if ((Success) && (!(this->Quit = new Text(DrawDevice, (UINT)ScreenWidth / 22, FALSE, L"Times New Roman"))))
		Success = FALSE;
	else
	{
		this->Quit->SetText("Quit");
		this->Quit->SetColor(COLOR(255, 0, 0));
		this->Quit->SetLocation((UINT)((ScreenWidth / 2) - (ScreenWidth / 25)), (UINT)((ScreenHeight / 2) + (ScreenHeight / 12)));
	}

	return Success;
}

BOOL MainMenu::Logic (GameKeyboard & Keyboard, GameState & State)
{
	BOOL Success = TRUE;

	Keyboard.Update();
	if (Keyboard.IsKeyDown(DIK_ESCAPE))
		Success = FALSE;
	else if ((this->SelectedText == "Play Game") && (Keyboard.IsKeyDown(DIK_DOWN)))
		this->SetSelectedText("Quit");
	else if ((this->SelectedText == "Quit") && (Keyboard.IsKeyDown(DIK_UP)))
		this->SetSelectedText("Play Game");

	if ((this->SelectedText == "Play Game") && (Keyboard.IsKeyDown(DIK_RETURN)))
		State = InGame;
	else if ((this->SelectedText == "Quit") && (Keyboard.IsKeyDown(DIK_RETURN)))
		Success = FALSE;

	return Success;
}

void MainMenu::Render (void)
{
	this->Background->Draw();
	this->Title->Draw();
	this->PlayGame->Draw();
	this->Quit->Draw();
}

void MainMenu::SetSelectedText (LPSTR Text)
{
	if (Text == "Play Game")
	{
		this->SelectedText = Text;
		this->PlayGame->SetColor(COLOR(0, 255, 0));
		this->Quit->SetColor(COLOR(255, 0, 0));
	}
	else if (Text == "Quit")
	{
		this->SelectedText = Text;
		this->PlayGame->SetColor(COLOR(255, 0, 0));
		this->Quit->SetColor(COLOR(0, 255, 0));
	}
}
